<?php

class LoginAction extends fvAction {
	
	function __construct() {
		parent::__construct ( fvSite::$Layoult );
	}
	
	function executeIndex() {
		if (! fvRequest::getInstance ()->isXmlHttpRequest ()) {
			return self::$FV_OK;
		} else {
			return self::$FV_AJAX_CALL;
		}
	}
	
	function executeDeny() {
		if (! fvRequest::getInstance ()->isXmlHttpRequest ()) {
			return self::$FV_OK;
		} else {
			return self::$FV_AJAX_CALL;
		}
	}
	
	function executeRegister() {
		$request = fvRequest::getInstance ();
		$Users ['login'] = UserManager::getInstance ()->getCount ( 'login="' . $request->getRequestParameter ( 'login' ) . '"' );
		$Users ['email'] = UserManager::getInstance ()->getCount ( 'email="' . $request->getRequestParameter ( 'email' ) . '"' );
		$m ['login'] = $request->getRequestParameter ( 'login' );
		$m ['password'] = $request->getRequestParameter ( 'password' );
		$m ['password1'] = $request->getRequestParameter ( 'password1' );
		$m ['email'] = $request->getRequestParameter ( 'email' );
		$m ['active'] = 1;
		$request->putRequestParameter ( "m", $m );
		$User = new User ( );
		$User->updateFromRequest ( $m );
		if ($User->save ()) {
			$mail = new html_mime_mail ( );
			$letter = "<html><body>Спасибо за регистрацию на сайте <a href=\"http://turtesstv\">www.turtess.tv</a>.</body></html>";
			$mail->add_html ( $letter );
			$mail->build_message ( "w" );
			$mail->SendMail ( 'noreply@turtess.tv', 'Администрация', $m ['email'], $m ['login'], "Регистрация на сайте" ); 
			fvSite::$fvSession->set ( "ERROR", NULL );
			if (! $this->executeLogin ()){
				$this->redirect ( "@own/edit/" );
			}
		} else {
			fvSite::$fvSession->set ( "ERROR", $User->getValidationResult () );
		}
		if (fvRequest::getInstance ()->isXmlHttpRequest ())
			return self::$FV_NO_LAYOUT;
		else
			return self::$FV_OK;
	}
	
	function executeForget() {
		$request = fvRequest::getInstance ();
		if (strstr ( $request->getRequestParameter ( 'login_email' ), '@' )) {
			$Users = UserManager::getInstance ()->getCount ( 'email="' . $request->getRequestParameter ( 'login_email' ) . '"' );
			$User = UserManager::getInstance ()->getByEmail ( $request->getRequestParameter ( 'login_email' ) );
			if (is_object ( $User [0] ))
				$User = $User [0];
		} else {
			$Users = UserManager::getInstance ()->getCount ( 'login="' . $request->getRequestParameter ( 'login_email' ) . '"' );
			$User = UserManager::getInstance ()->getByLogin ( $request->getRequestParameter ( 'login_email' ) );
			if (is_object ( $User [0] ))
				$User = $User [0];
		}
		if ($Users == 0) {
			fvSite::$fvSession->set ( "ERROR", "По вашему запросу не найдено ни одного пользователя." );
		} else {
			$m ['password'] = uniqid ();
			$m ['password1'] = $m ['password'];
			$request->putRequestParameter ( "m", $m );
			$User->updateFromRequest ( $m );
			if ($User->save ()) {
				$mail = new html_mime_mail ( );
				$letter = "<html><body>Ваши данные на сайте <a href=\"http://turtesstv\">www.turtess.tv</a> успешно восстановлены.<p>
			Ваш логин: " . $User->login . "<br>
			Ваш новый пароль: " . $m ['password'] . "</body></html>";
				$mail->add_html ( $letter );
				$mail->build_message ( "w" );
				$mail->SendMail ( 'noreply@turtess.tv', 'Администрация', $m ['mail'], $m ['surname'] . " " . $m ['name'], "Регистрация на сайте" );
				fvSite::$fvSession->set ( "User", $User );
				fvSite::$fvSession->set ( "ERROR", NULL );
			} else {
				fvSite::$fvSession->set ( "ERROR", $User->getValidationResult () );
			}
		}
		if (fvRequest::getInstance ()->isXmlHttpRequest ())
			return self::$FV_NO_LAYOUT;
		else
			return self::$FV_OK;
	}
	
	function executeLogin() {
		$UserManager = UserManager::getInstance ();
		$request = fvRequest::getInstance ();
		$LoggedManager = $UserManager->Login ( $request->getRequestParameter ( "login" ), $request->getRequestParameter ( 'password' ) );
		if ($LoggedManager instanceof User) {
			
			fvSite::$fvSession->setUser ( $LoggedManager );
			if (fvSite::$fvSession->is_set ( "login/redirectURL" ))
				$this->redirect ( "@#" . fvSite::$fvConfig->get ( 'dir_web_root' ) . fvSite::$fvSession->get ( "login/redirectURL" ) );
			elseif (! $LoggedManager->get ( full_name ))
				$this->redirect ( "@own/edit/" );
			else
				$this->redirect ( "@" );
		} else {
			fvSite::$fvSession->set ( 'ERROR_LOGIN', "Имя пользователя и пароль не соответствуют" );
			$this->redirect ( '@' );
		}
	}
	
	function executeLogout() {
		fvSite::$fvSession->setUser ( false );
		$this->redirect ( "" );
	}
}

?>
